use "created_dta\analysis_samp_w_outcomes_incw", clear
drop if female == .

preserve 
use "raw_dta\cas_unit_deploy_nopii_v3.dta", clear
keep pid dodgrp incident_city incident_country incident_state incidentdt_s dnu2 compt suicide death
sort pid incidentdt_s
by pid: gen id_n = _n
gen incident_myr = ym(year(incidentdt_s),month(incidentdt_s))
reshape wide dodgrp incident_city incident_country incident_state incidentdt_s incident_myr dnu2 compt suicide death, i(pid) j(id_n)
tempfile cas
save `cas'
restore 
merge 1:1 pid using `cas', nogen keep(1 3)

gen cas_death_myr = incident_myr1 if death1 == 1
gen cas_death_country = incident_country1 if death1 == 1
forvalues i = 1/5 {
	replace cas_death_myr = incident_myr`i' if death`i' == 1
	replace cas_death_country = incident_country`i' if death1 == `i'
}


egen mos_post_yr_terms = group(init_pmos post_orgraa init_terms year_arrival)
egen mos_tc_post_yr_terms = group(init_pmos_cons post_orgraa init_terms year_arrival)
egen mos_tc_post_qtr_terms = group(init_pmos_cons post_orgraa init_terms qtr)
egen dmsl_qtr = group(dmsl_tc_first qtr)
egen dmsl_month = group(dmsl_tc_first month_arrival)
egen dmsl_yr = group(dmsl_tc_first year_arrival)
g all = 1 
g terms3 = init_terms == "3"

g bal_samp = (year_arrival<=2011)


g anydepc = init_nrdepc > 0 & init_nrdepc < . 


forvalues y = 1(1)36 {
 g noncom_death_by_`y' = ndi_death_by_`y' - combat_death_by_`y'
 replace noncom_death_by_`y' = 0 if noncom_death_by_`y' == -1
 g ndi_nondespnoncom_by_`y' = ndi_death_by_`y' - ndi_war_by_`y' - ndi_despair2_by_`y'
g despairv3_by_`y' = (ndi_drug2_by_`y' == 1 | ndi_suicide_plus2_by_`y' ==1  )
 g intentdrug_by_`y' = (ndi_drug_self2_by_`y' == 1 | ndi_drug_und2_by_`y' == 1) 
  g ndi_suic_by_`y' = ndi_suicide_plus2_by_`y'
    g wia_kia_by_`y' = max(wia_by_`y', kia_by_`y')
  
  g inc_combined_by_`y' = (inc_by_`y' == 1 | incarcerated_by_`y' == 1) if inc_by_`y' != .
  
  g anyssdissi_in_`y' = anyssdi_in_`y' == 1 | anyssi_in_`y' == 1
  
  foreach dv in profile sigprofile phys sigphys psychiatric sigpsychiatric othprof sigothprof {
  	replace any_`dv'_by_`y' = 0 if missing(any_`dv'_by_`y')
  }  
  }
  
  replace any_premier_v1_2_all2870689 = 0 if premier_v1_2_all2870689 == 99
  replace any_premier_v1_2_all2875689 = 0 if premier_v1_2_all2875689 == 99
  replace any_p13_all2870731 = 0 if p13_all2870731 == 99
  replace any_p13_all2875731 = 0 if p13_all2875731 == 99

  replace premier_v1_2_all2870689 = 0 if premier_v1_2_all2870689 == 99
  replace premier_v1_2_all2875689 = 0 if premier_v1_2_all2875689 == 99
  replace p13_all2870731 = 0 if p13_all2870731 == 99
  replace p13_all2875731 = 0 if p13_all2875731 == 99
 
g mths_dep_win_3yr = deployed_0 + deployed_1 + deployed_2 + deployed_3 ///
+ deployed_4 + deployed_5 + deployed_6 + deployed_7 ///
+ deployed_8 + deployed_9 + deployed_10 + deployed_11 ///
+ deployed_12 + deployed_13 + deployed_14 + deployed_15 ///
+ deployed_16 + deployed_17 + deployed_18 + deployed_19 ///
+ deployed_20 + deployed_21 + deployed_22 + deployed_23 ///
+ deployed_24 + deployed_25 + deployed_26 + deployed_27 ///
+ deployed_28 + deployed_29 + deployed_30 + deployed_31 ///
+ deployed_32 + deployed_33 + deployed_34 + deployed_35

bys dmsl_tc_first qtr : egen meandep = mean(mths_dep_win_3yr)  
bys dmsl_tc_first qtr: egen ct = total(1) 
g instrument_mths_dep_qtr = ((meandep*ct) - mths_dep_win_3yr)/(ct-1)

gen kia_by_12_adj = kia_by_12 
replace kia_by_12_adj = 0 if dep_win_3yr == 0  
bys dmsl_tc_first qtr: egen meankia = mean(kia_by_12_adj) 
g kiarate = ((meankia*ct) - kia_by_12_adj)/(ct - 1)
		assert kiarate>-.000001
		replace kiarate = 0 if kiarate<0 & kiarate>-.000001
		
gen wia_by_12_adj = wia_by_12 
replace wia_by_12_adj = 0 if dep_win_3yr == 0 
bys dmsl_tc_first qtr: egen meanwia = mean(wia_by_12_adj) 
gen wiarate = ((meanwia*ct) - wia_by_12_adj)/(ct-1)
		assert wiarate>-.000001 
		replace wiarate = 0 if wiarate<0 & wiarate>-.000001

	gen combat_death_by_12_adj = combat_death_by_12 
replace combat_death_by_12_adj = 0 if dep_win_3yr == 0  
bys dmsl_tc_first qtr: egen meancombat = mean(combat_death_by_12_adj) 
g comrate = ((meancombat*ct) - combat_death_by_12_adj)/(ct - 1)
		assert comrate>-.000001
		replace comrate = 0 if comrate<0 & comrate>-.000001
	
	gen cas_by_12_adj = (combat_death_by_12_adj==1|wia_by_12_adj==1)
	bys dmsl_tc_first qtr: egen meancas = mean(cas_by_12_adj) 
	gen casrate = ((meancas*ct) - cas_by_12_adj)/(ct-1)

foreach n in cas kia wia com {
	gen `n'rate_notsd=`n'rate
	sum `n'rate
	replace `n'rate = (`n'rate - r(mean))/r(sd)
}

drop meandep ct 

foreach n in kia wia cas com {
	gen ${NEWINST}_`n'rate = ${NEWINST} * `n'rate
	gen ${NEWENDOG}_`n'rate = ${NEWENDOG} * `n'rate
}

	gen ${NEWINST}_casnotsd = ${NEWINST} * `n'casrate_notsd *100
	gen ${NEWENDOG}_`n'casnotsd = ${NEWENDOG} * `n'casrate_notsd * 100
	gen casnotsd = casrate_notsd * 100
	
bys dmsl_tc_first month_arrival : egen meandep = mean(mths_dep_win_3yr)
bys dmsl_tc_first month_arrival: egen ct = total(1)
g instrument_mths_dep_mth = ((meandep*ct) - mths_dep_win_3yr)/(ct-1)
drop meandep ct 

bys dmsl_tc_first year_arrival : egen meandep = mean(mths_dep_win_3yr)
bys dmsl_tc_first year_arrival: egen ct = total(1)
g instrument_mths_dep_yr = ((meandep*ct) - mths_dep_win_3yr)/(ct-1)
drop meandep ct 

bys dmsl_tc_first qtr : egen meandep = mean(dep_win_3yr)
bys dmsl_tc_first qtr: egen ct = total(1)
g instrument_any_dep_qtr = ((meandep*ct) - dep_win_3yr)/(ct-1)
drop meandep ct 

g mths_dep_by_q1 = deployed_0 + deployed_1 + deployed_2
forvalues x = 2(1)36 {
	local i = 3*(`x'-1)
	local j = `i' + 1
	local k = `j' + 1
	local z = `x' - 1
	g mths_dep_by_q`x' = deployed_`i' + deployed_`j' + deployed_`k' + mths_dep_by_q`z'
} 

gen init_cmf = substr(init_pmos_cons,1,2)
foreach x in 13 19 68 91 92 {
g init_mos`x' = init_cmf == "`x'"
}

egen mos_bct_yr_terms = group(init_pmos_cons bct_type year_arrival init_terms)
egen mos_bct_qtr_terms = group(init_pmos_cons bct_type qtr init_terms)
g init_afqsc_2 = init_afqsc^2
g init_afqsc_3 = init_afqsc^3

foreach ls in cl co el fa gm gt mm of sc { 
	g `ls'_mi = ace_first_asvab`ls' == .
	replace ace_first_asvab`ls' = 0 if ace_first_asvab`ls' == .
}


gen ${NEWENDOG}_2=${NEWENDOG}^2
gen ${NEWINST}_2=${NEWINST}^2

gen casrate_permo=casrate_notsd/${NEWINST}
sum casrate_permo, d
gen cascat1=(casrate_permo<`r(p50)')
gen cascat2=(casrate_permo>=`r(p50)' & casrate_permo!=.)

forvalues k=1(1)2{
	gen ${NEWENDOG}_cascat`k'=${NEWENDOG}*cascat`k'
	gen ${NEWINST}_cascat`k'=${NEWINST}*cascat`k'
}

gen anycas=(casrate_notsd>0)
	gen ${NEWENDOG}_anycas=${NEWENDOG}*anycas
	gen ${NEWINST}_anycas=${NEWINST}*anycas

foreach var of varlist combat_death_by_* noncom_death_by_* ndi_war_by_* ndi_despair2_by_* ndi_suicide_plus2_by_* ndi_drug2_by_* ndi_mv_acc2_by_* ndi_other2_by_* ndi_firearm2_by_* ndi_assault2_by_*   anydisability_in_* anyvadc_in_* anyssdissi_in_*  ndi_death_by_* kia_by_* wia_by_* any_sigprofile_by_* sep_by_*  bar_or_misconduct_by_* any_profile_by_* nsc_enr_postarr_by_* nsc_asscplus_postarr_by_* nsc_bachplus_postarr_by_*  bar_or_misconduct_by_* inc_combined_by_* nsc_enr_pa_by_2017 nsc_enr_pa_by_2020 nsc_asscplus_pa_by_2017 nsc_asscplus_pa_by_2020 nsc_bachplus_pa_by_2017 nsc_bachplus_pa_by_2020 nsc_enroll_by_2017 nsc_enroll_by_2020 nsc_asscplus_by_2017 nsc_asscplus_by_2020 nsc_bachplus_by_2017 nsc_bachplus_by_2020  {
	count if `var' != 0 & `var' != 1 & `var' != .
	if r(N) == 0 replace `var' = `var' * 100
}


sort pid 
save "created_dta\analysis_samp_w_outcomes_final_incw", replace 


